package activity;

import modelo.Cliente;
import modelo.Funcionario;

import persistencia.WebServices;
import teste.exemplo.ws.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import auxiliar.Utilitarios;

public class LoginActivity extends Activity implements View.OnClickListener {
	
	EditText strLogin = null;
	EditText strSenha = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		Button b = (Button) findViewById(R.id.buttonLogar);
		b.setOnClickListener(this);

//		Button c = (Button) findViewById(R.id.buttonClienteTeste);
//		c.setOnClickListener(this);

		strLogin = (EditText) findViewById(R.id.editTextLogin);
		strSenha = (EditText) findViewById(R.id.editTextSenha);
		
		/*
		MyLocationListener lp = new MyLocationListener(this);
		lp.retrieveLocation();
		*/

	}

//	@Override
//	protected void onResume() {
//		super.onResume();
//		LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//
//		if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//			createGpsDisabledAlert();
//		}
//	}

	public void onClick(View v) {
		
		try{
	
		switch (v.getId()) {
		case R.id.buttonLogar:
			
			if(this.strLogin.getText().toString().trim().length() == 0)
				throw new Exception("Login não informado!");	
			
			if(this.strSenha.getText().toString().trim().length() == 0)
				throw new Exception("Senha não informado!");
			
			String strLogin = this.strLogin.getText().toString();
			String strSenha = Utilitarios.md5(this.strSenha.getText().toString());
			
			WebServices ws = new  WebServices();
			Funcionario oFuncionario = ws.logar(strLogin, strSenha);
			
			if(oFuncionario != null){
				startActivity(new Intent(getBaseContext(),
									OsListViewActivity.class));
			}else
				throw new Exception("Usuário Inválido!");
			break;
			
			
//		case R.id.buttonClienteTeste:
//			
//			Cliente cliente = new Cliente();
//			cliente.setIdCliente("10");
//			cliente.setNome("CLIENTE");
//			cliente.setEndereco("RUA SEM NOME");
//			
//			Intent it = new Intent(getBaseContext(), ClienteActivityTeste.class);
//			
//			it.putExtra("cliente", cliente);
//
//			startActivity(it);
//			//startActivity(new Intent(getBaseContext(), ClienteActivity.class));
//			break;
		default:
			break;
		}
		
	}catch(Exception ex){
		Toast.makeText(getApplicationContext(),
				ex.getMessage(), Toast.LENGTH_LONG).show();

		}
	}

//	private void createGpsDisabledAlert() {
//		AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		builder.setMessage("O GPS est‡ desabilitado! Voc� gostaria de habilit‡-lo?")
//				.setCancelable(false)
//				.setPositiveButton("Ativar GPS",
//						new DialogInterface.OnClickListener() {
//							public void onClick(DialogInterface dialog, int id) {
//								showGpsOptions();
//							}
//						});
//		AlertDialog alert = builder.create();
//		alert.show();
//	}
//
//	private void showGpsOptions() {
//		Intent gpsOptionsIntent = new Intent(
//				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//		startActivity(gpsOptionsIntent);
//	}
}